package cn.com.cfae.iras.doc.analyze.parse.tokenizer;

import cn.com.cfae.iras.doc.analyze.parse.QYZXUtils;
import cn.com.cfae.iras.doc.analyze.parse.kv.KVText;
import cn.com.cfae.iras.doc.analyze.parse.kv.TextLayout;
import cn.com.cfae.iras.doc.analyze.parse.model.ExtractBizModel;
import cn.com.cfae.iras.doc.analyze.parse.model.ExtractItem;
import cn.com.cfae.iras.doc.analyze.parse.model.SectionModel;
import com.hankcs.hanlp.corpus.document.sentence.Sentence;
import com.hankcs.hanlp.corpus.document.sentence.word.IWord;
import com.hankcs.hanlp.dictionary.CustomDictionary;
import com.hankcs.hanlp.tokenizer.NLPTokenizer;
import org.apache.commons.beanutils.BeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class QYZXTokenizer extends BaseTokenizer {

    private static QYZXTokenizer instance = new QYZXTokenizer();


    public static QYZXTokenizer getInstance() {
        return instance;
    }

    private QYZXTokenizer() {
    }

    private static Logger logger = LoggerFactory.getLogger(QYZXTokenizer.class);

    /**
     * 目前采用该方式
     *
     * @param extractBizModel
     * @param
     * @return
     */
    public ExtractItem splitWords3(ExtractBizModel extractBizModel, SectionModel childSectionModel) {
        String extractText = childSectionModel.getContent();
        logger.info("开始分析信用评级机构，进行分词分析，内容：{}.......", extractText);
        QYZXUtils.getDefaultXypjjg().forEach(e -> {
            if (CustomDictionary.get(e) == null) {
                CustomDictionary.add(e);
            }
        });
        Sentence sentence = NLPTokenizer.ANALYZER.analyze(extractText);
        ExtractItem extractItem = null;
        extractItem = extractBizModel.getExtractItem("MC");
        if (extractItem.getKey() != null && extractItem.getValue() != null) {
            return extractItem;
        }
        List<IWord> wordList = sentence.wordList;
        logger.info("抽取信用评级机构，分析文本：{}，列表：{}。", extractText, wordList);
        IWord iWord = null;
        for (int i = 0; i < wordList.size(); i++) {
            iWord = wordList.get(i);
            String word = iWord.getValue();
            if (QYZXUtils.getDefaultXypjjg().contains(word)) {
                KVText kText = new KVText();
                KVText vText = new KVText();
                kText.setText(iWord.getValue());
                vText.setText(iWord.getValue());
                TextLayout kTextLayout = new TextLayout();
                TextLayout vTextLayout = new TextLayout();
                kTextLayout.setLeft(childSectionModel.getTextLayout().getLeft());
                kTextLayout.setTop(childSectionModel.getTextLayout().getTop());
                kTextLayout.setPageNumber(childSectionModel.getTextLayout().getPageNumber());
                vTextLayout.setLeft(childSectionModel.getTextLayout().getLeft());
                vTextLayout.setTop(childSectionModel.getTextLayout().getTop());
                vTextLayout.setPageNumber(childSectionModel.getTextLayout().getPageNumber());
                kTextLayout.setText(childSectionModel.getTitle());
                vTextLayout.setText(iWord.getValue());
                kText.add(kTextLayout);
                vText.add(vTextLayout);
                extractItem.setKey(kText);
                extractItem.setValue(vText);
                logger.info("分析到信用评级机构信息，业务指标名称：{}，ItemName：{}。", word, extractItem.getItemName());
                break;
            }
        }
        logger.info("开始分析信用评级机构，分析完成。");
        return extractItem;
    }

}
